package com.jsmtr.rms.controller.DoZZCJ;

import com.jsmtr.rms.entity.BaseUser;
import com.jsmtr.rms.entity.DUserResume;
import com.jsmtr.rms.entity.ProjectInfoFlow;
import com.jsmtr.rms.entity.ProjectInfoJobs;
import com.jsmtr.rms.service.IDoGXService;
import com.jsmtr.rms.service.IUtilService;
import com.jsmtr.rms.utils.EnumTools;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;


@Controller
@RequestMapping("/Do/ZZCJPost")
public class DoZZCJPostController {

    @Resource
    private IUtilService utilService;

    @Resource
    private IDoGXService doGXService;


    //[start] 查询毕业生信息

    //首页
    @RequestMapping("/")
    public String Index(HttpServletRequest request, Model model) {
        try {
            model.addAttribute("path", request.getContextPath());
            String pid = request.getParameter("pid").trim();
            String fid = request.getParameter("fid").trim();
            String deptcode = request.getParameter("deptcode").trim();

            model.addAttribute("pid", pid);
            model.addAttribute("fid", fid);
            model.addAttribute("deptcode", deptcode);

            //名族
            model.addAttribute("minzu", utilService.optionList("B", "0"));
            //政治面貌
            model.addAttribute("zzmm", utilService.optionList("E", "0"));
            //最高学历
            model.addAttribute("education", utilService.optionList("F", "0"));
            //招聘地区
            model.addAttribute("regions", utilService.optionList("M", "0"));
            //招聘岗位
            List<ProjectInfoJobs> projectJobs=utilService.getProjectJobs(pid);
            model.addAttribute("jobs",projectJobs);
			
			/*StringBuilder str2 = new StringBuilder();
			List<Object[]> jobs = doGXService.getDemands(pid);
			for (Object[] job : jobs) {
				str2.append("<option value='"+job[0]+"'>"+job[1]+"</option>");
			}
			model.addAttribute("jobs", str2);*/
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Do/ZZCampusSpring/Post/index";
    }

    @SuppressWarnings("unchecked")
    @RequestMapping("/getusers")
    public void getusers(HttpServletRequest request, HttpServletResponse response, DUserResume user) {
        try {
            int perpage = Integer.parseInt(request.getParameter("perpage"));
            int page = Integer.parseInt(request.getParameter("page"));
            String flowid = request.getParameter("flowid").trim();
            user.setPath("/" + flowid + "/");

            Map<String, Object> _map = new HashMap<String, Object>();
            _map.put("agefrom", request.getParameter("agefrom").trim());
            _map.put("ageto", request.getParameter("ageto").trim());
            _map.put("heightfrom", request.getParameter("heightfrom").trim());
            _map.put("heightto", request.getParameter("heightto").trim());

            Map<String, Object> map = doGXService.getPostUsers(page, perpage, user, _map);

            StringBuilder result = new StringBuilder();
            double pageCount = (Double) map.get("pageCount");
            double allcount = (Double) map.get("count");

            if (pageCount != 0) {
                Object object = map.get("list");
                for (Object[] _user : (List<Object[]>) object) {
                    //r.id,r.code,r.name,r.sexname,r.cardaddress,r.university,r.majorname,r.educationname,r.tel,
                    //s.point,r.jname,r.gwstate,r.fid,r.fqstate,r.op6
                    result.append("<tr><td><input type=\"checkbox\" name='uid' value='" + _user[0] + "' id='cb_" + _user[0] + "' /></td>");
                    result.append("<td>" + _user[1] + "</td>");
                    result.append("<td class='link'><a target='_blank' href='"
                            + request.getContextPath() + "/UserResume/ViewDResume?id="
                            + _user[0] + "'>" + _user[2]
                            + "</a></td>");
                    result.append("<td>" + _user[3] + "</td>");
                    /*result.append("<td>" +  _user[4] + "</td>");*/
                    result.append("<td>" + _user[5] + "</td>");
                    result.append("<td>" + _user[6] + "</td>");
                    result.append("<td>" + _user[7] + "</td>");
                    result.append("<td>" + _user[8] + "</td>");
                    result.append("<td>" + _user[9] + "</td>");
                    result.append("<td>" + (_user[11].equals("未分配") ? _user[11] : _user[10]) + "</td>");
                    if (_user[14] == null || _user[14].equals("未公布")) {
                        result.append("<td class='true'><span>未公布</span></td>");
                    } else {
                        result.append("<td class='false'><span>已公布</span></td>");
                    }

                    if (_user[13].equals("已放弃") && _user[12].equals(flowid)) {
                        result.append("<td class='yfq'><span>已放弃</span></td></tr>");
                    } else {
                        if (_user[11].equals("未分配")) {
                            result.append("<td class='true'><span>未完成</td></tr>");
                        } else {
                            if (_user[12].equals(flowid)) {
                                result.append("<td class='true'><span>未完成</td></tr>");
                            } else {
                                result.append("<td class='false'><span>已完成</td></tr>");
                            }
                        }
                    }
                }
            } else {
                result.append("<tr><td colspan='13'>无符合条件的数据</td></tr>");
            }
            result.append("<input type='hidden' id='pageCount' value='" + pageCount + "' />");
            result.append("<input type='hidden' id='allCount' value='" + allcount + "' />");
            utilService.printTextResult(result.toString(), response);
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printTextResult("<tr><td colspan='13'>读取数据失败</td></tr><input type='hidden' id='pageCount' value='0' /><input type='hidden' id='allCount' value='0' />", response);
        }

    }
    //[end]

    //[start] 岗位分配

    @SuppressWarnings({"unused", "rawtypes"})
    @RequestMapping(value = "/PostAction", method = RequestMethod.POST)
    public void RAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String fid = request.getParameter("fid").trim();//招聘流程ID
            String hrcode = utilService.getConfig("hrcode");//招聘主管部门编码

            BaseUser cuser = utilService.getSessionUser(request);
            if (cuser.getUdept().equals(hrcode)) {
                //流程是否结束
                if (utilService.FlowIsDoing(fid)) {
                    List editlist = new ArrayList();
                    Date time = new Date();
                    String gwcode = request.getParameter("jobcode").trim();//BaseJob的code
                    String gwname = request.getParameter("jobname").trim();//BaseJob的name
                    String[] uids = request.getParameterValues("uid");
                    if (uids == null) {
                        utilService.printJsonResult(false, "请选择人员！", "no", response);
                    } else {
                        List<DUserResume> editList = new ArrayList<DUserResume>();
                        List<DUserResume> users = doGXService.getResumes(uids);
                        for (DUserResume user : users) {
                            if (user.getFqstate().equals("未放弃")
                                    && user.getFid().equals(fid)) {

                                user.setJid(gwcode);
                                user.setJname(gwname);
                                user.setGwdate(time);
                                user.setGwuid(cuser.getId());
                                user.setGwuname(cuser.getUname());
                                user.setGwstate("已分配");

                                editList.add(user);
                            }
                        }

                        if (editList.size() > 0) {
                            utilService.editList(editList);
                            utilService.printJsonResult(true, "操作成功！", "no", response);
                        } else {
                            utilService.printJsonResult(false, "当前选中信息不支持此操作！", "no", response);
                        }
                    }
                } else {
                    utilService.printJsonResult(false, "当前流程不支持此操作！", "no", response);
                }
            } else {
                utilService.printJsonResult(false, "您无权执行此操作！", "no", response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }
    }

    //[end]

    //[start] 流程扭转到下一步

    @SuppressWarnings({"rawtypes", "unchecked"})
    @RequestMapping(value = "/NextAction", method = RequestMethod.POST)
    public void NextAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String[] ids = request.getParameterValues("id");//人员信息ID
            if (ids != null) {
                String pid = request.getParameter("pid").trim();//招聘方案ID
                String fid = request.getParameter("fid").trim();//招聘流程ID
                String deptcode = request.getParameter("deptcode").trim();//责任部门编码
                BaseUser usr = utilService.getSessionUser(request);
                if (usr.getUdept().equals(deptcode)) {
                    //流程是否结束
                    if (utilService.FlowIsDoing(fid)) {
                        List editlist = new ArrayList();
                        List<DUserResume> users = doGXService.getResumes(ids);
                        ProjectInfoFlow nFlow = doGXService.getNextFlow(pid, deptcode, fid);

                        for (DUserResume user : users) {
                            if (user.getFid().equals(fid)
                                    && user.getFqstate().equals("未放弃")
                                    && "已分配".equals(user.getGwstate())) {
                                user.setFid(nFlow.getId());
                                user.setPath(user.getPath() + nFlow.getId() + "/");
                                editlist.add(user);
                            }
                        }

                        if (editlist.size() > 0) {
                            nFlow.setState(EnumTools.PlanFlowState.进行中.ordinal());
                            editlist.add(nFlow);

                            utilService.editList(editlist);
                            utilService.printJsonResult(true, "操作成功！", "no", response);
                        } else {
                            utilService.printJsonResult(false, "当前选中的信息不能执行此操作！", "no", response);
                        }
                    } else {
                        utilService.printJsonResult(false, "当前流程不支持此操作！", "no", response);
                    }
                } else {
                    utilService.printJsonResult(false, "您无权执行此操作！", "no", response);
                }
            } else {
                utilService.printJsonResult(false, "请选择人员信息！", "no", response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }
    }

    //[end]
}
